import { objDeepCopy,getDateString } from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.getAllShopList(this);
		this.getShopselect(this);
	},
	mounted() {
		
	},
	activated() {
		this.getAllShopList(this);
	},
	methods: {
		//获取列表接口
		getAllShopList(that,params,pageIndex = 1,pageSize = 10) {
			params = params || {countType:1,inventoryDirection:2};
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whInventoryMain/list',
				//url:'/api/whInventoryMain/list',
				data:{
					condition:params,
					pageIndex: pageIndex,
					pageSize: pageSize,
				}
			}).then((res)=>{
				that.Model.tableData = [];
				var data = res.data;
				if(data.status == 200){
					var obj = {};
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					var result = data.result.result;
					console.log(result)
					if(result){
						for (var i = 0; i < result.length; i++) {
							obj = objDeepCopy(result[i]);
							//盘点状态
                            var inventoryStatusName;
                            switch (result[i].inventoryStatus){
                                case 1:inventoryStatusName = '未开始'
                                    break;
                                case 2:inventoryStatusName = '盘点中'
                                    break;
                                case 3:inventoryStatusName = '待提交'
                                    break;   
                                case 4:inventoryStatusName = '已完结'
                                    break;  
                                case 5:inventoryStatusName = '已过账'
                                    break;   
                                default:
                                    break;
                            }
                            obj.inventoryStatusName = inventoryStatusName;
							that.Model.tableData.push(obj);
						}
					}
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//下拉框
		getShopselect(that) {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whInventoryMain/getInfoByUserId',
				//url:'/api/whInventoryMain/getInfoByUserId',
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					console.log(result)
					
					let shopArr = [], locationsArr = [], infosArr = [];
					for (let i in result.shops) {//盘点地点（工厂）
						shopArr.push({ label: result.shops[i].theNameOfTheStore, value: result.shops[i].theFactory});
                    };
                    for (let i in result.Infos) {//供应商
						infosArr.push({ label: result.Infos[i].theNameOfTheOne, value: result.Infos[i].accountNumberOfSupplierOrCreditor});
					};
					for (let i in result.locations) {//盘点库位
						locationsArr.push({ label: result.locations[i].descriptionOfWarehouseLocation, value: result.locations[i].theInventoryLocation});
					};
					this.Model.filterOption.items[0].data = shopArr;//盘点地点
                    this.Model.filterOption.items[1].data = locationsArr;//盘点库位
                    this.Model.allfactory = shopArr;
					this.Model.infosArr = infosArr;
                    this.Model.alllocation = locationsArr;
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//跳转详情
		tableDetail(row,flag,status) {
			this.$router.push({
				path:'goodsInventoryDetail',
				query: {
					id:row.id,
					flag,
					status
				}
			});
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		//新建盘点任务
		Toaddnew() {
			this.Model.Addnewtask = true;
		},
		//确认新建
		sureAddtask() {
            let params ={};
			let that = this;
            let Location = this.Model.Inventorylocation.toString();
            params.createBy = 131313;	//创建人
			params.supplierAccount = this.Model.defaultInfo;// 供应商编码
			params.specialStockMark = this.Model.defaultOt;// 特殊库存标识
			params.factory = this.Model.Inventoryfactory;// 工厂
			params.inventoryLocation = Location;// 库存地点  
			params.countType = 1;	//盘点物品类型 1.商品盘点 2.包材盘点 3.抽盘
			params.inventoryDirection = 2;   //盘点类型 1.门店  2.供应链
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whInventoryMain',
				// url:'/api/whInventoryMain',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					setTimeout(function(){
						that.tips(that,'success','新建成功！');
						that.Model.Addnewtask = false;
						that.getAllShopList(that);
					},1000);
					
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
        },
        //结束盘点
        endInventory(row,that,type) {
            if(type == '结束盘点'){
                this.$confirm('结束盘点后会生成盘点结果，无法再对该盘点进行修改,是否确定结束该盘点任务？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.endInventoryAjax(row.id,3);
                }).catch(() => {
                    that.tips(that,'warning','取消成功')     
                });
            }else{
                this.$confirm('是否确定反审核？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.endInventoryAjax(row.id,2);
                }).catch(() => {
                    that.tips(that,'warning','取消成功')     
                });
            }
            
        },
        //结束、反审核请求
        endInventoryAjax(id,status) {
            var that = this;
            this.$ajax({
                method: 'put',
                url:this.apiTest+'/supplier/whInventoryMain',
                // url:'/api/whInventoryMain',
                data:{
                    id:id,
                    inventoryStatus:status
                }
            }).then((res)=>{
                var data = res.data;
                if(data.status == 200){
                    that.tips(that,'success',data.result);
                    that.getAllShopList(this);
                }else{
                    that.tips(that,'warning',data.msg)
                }
            
            });
        },
        //提交报告
        submitReport(row) {
            var submitter = 1;
            var MainId = row.id;
			this.$ajax({
				method: 'post',
				url:this.apiTest+`/supplier/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`,
				//url:`/api/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
                    this.tips(this,'success','提交报告成功')
				}	
			});
        },
		handleSelectionChange(val) {
			console.log(val);
		},
		handleCurrentChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllShopList(this,searchFromData,val);
		},
		handleSizeChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllShopList(this,searchFromData,undefined,val);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
            var obj = val;
            obj.countType = 1;
            obj.inventoryDirection = 2;
            //开始盘点时间
			if(val.inventoryStart){
				obj.inventoryStartTime  = getDateString(val.inventoryStart[0]);
                obj.inventoryStartToTime = getDateString(val.inventoryStart[1]);
                val.inventoryStart = undefined;
            }
            //结束盘点时间
			if(val.inventoryEnd){
				obj.inventoryEndTime = getDateString(val.inventoryEnd[0]);
                obj.inventoryEndToTime = getDateString(val.inventoryEnd[1]);
                val.inventoryEnd = undefined;
            }
            //创建盘点时间
			if(val.create){
				obj.createTime = getDateString(val.create[0]);
                obj.createToTime = getDateString(val.create[1]);
                val.create = undefined;
            }
            //提交盘点时间
			if(val.submitInventory){
				obj.submitInventoryTime = getDateString(val.submitInventory[0]);
                obj.submitInventoryToTime = getDateString(val.submitInventory[1]);
                val.submitInventory = undefined;
            }
            this.Model.searchFromData = obj;
		},
		toSearch(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllShopList(this,searchFromData);
		},
		toClear(val) {
			this.Model.searchFromData = _.cloneDeep(this.Model.defaultSearchFromData);
			this.Model.filterOption = _.cloneDeep(this.Model.defaultFilterOption);
		},
		toExport(val) {
			console.log(val);
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	}
}
export default Controller;